using SharpGraphT.Sharp.System.Collections.Generic;

namespace SharpGraphT.Graph; 

public class UniformIntrusiveEdgesSpecifics<TV, TE> :
    BaseIntrusiveEdgesSpecifics<TV, TE, IntrusiveEdge>,
    IIntrusiveEdgesSpecifics<TV, TE> {
    private static readonly long SerialVersionUid = -5736320893697031114L;

    public UniformIntrusiveEdgesSpecifics(IDictionary<TE, IntrusiveEdge> map)
        : base(map) { }

    public override bool Add(TE e, TV sourceVertex, TV targetVertex) {
        if (e is IntrusiveEdge ie) {
            return AddIntrusiveEdge(e, sourceVertex, targetVertex, ie);
        }

        var previousSize = edgeMap.Count;
        var intrusiveEdge =
            edgeMap.ComputeIfAbsent(e, i => new IntrusiveEdge());
        if (previousSize >= edgeMap.Count) {
            return false;
        }

        intrusiveEdge.source = sourceVertex;
        intrusiveEdge.target = targetVertex;
        return true;
    }

    protected override IntrusiveEdge GetIntrusiveEdge(TE e) =>
        e is IntrusiveEdge ie ? ie : edgeMap[e];
}